package PreProcessData;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import Classes.Path;
/**
 * This is for INFSCI-2140 in 2020
 *  
 * The class for trectext format
 */
public class TrectextCollection implements DocumentCollection {

	// YOU CAN ADD ESSENTIAL PRIVATE METHODS OR VARIABLES -> 
    private BufferedReader br = null; // Used in both constructor and nextDocument method, so it is defined as a member variable
    private StringBuilder sb = null; 
    
	// YOU SHOULD IMPLEMENT THIS METHOD ->
	public TrectextCollection() throws IOException {
		// This constructor should open the file existing in Path.DataTextDir
		// and also should make preparation for function nextDocument().
		// You cannot load the whole corpus into memory here!!!
		FileInputStream fis = new FileInputStream(Path.DataTextDir); // Path.DataTextDir | "test/test_trectext" 
		InputStreamReader isr = new InputStreamReader(fis); // Using the default charset in consistency with the corpus
		br = new BufferedReader(isr);
	}

	// YOU SHOULD IMPLEMENT THIS METHOD ->
	public Map<String, Object> nextDocument() throws IOException {
		// This method should load one document from the corpus, and return this document's number and content.
		// The returned documents should never be returned again.
		// When no document left, return null.
		// Note: Remember to close the file that you opened, when you do not use it any more.
		Map<String, Object> textDoc = new HashMap<>(); // Store all docs with their ID and content
	    String line = "";       // Every line read by BufferedReader
	    String textDocID = "";      // The doc's number 
	    String textDocContent = ""; // The doc's content
	 
	    while((line = br.readLine()) != null) { // Continue reading a line while the end of stream has not been reached
	    	                                                                                                                
	        if("<DOC>".equals(line)) { // If "<DOC>" is found 
	        	line = br.readLine(); // Read the next line containing "<DOCNO>"
	        	textDocID = line.substring(8, 24); // Extract the doc's ID out
	            //System.out.println("textDocID: " + textDocID);
	        } 
	        
	        if("<TEXT>".equals(line)) { // If "<TEXT>" is found
	        	sb = new StringBuilder();
			    while(!("</TEXT>").equals(line = br.readLine())) { // while </TEXT> line has not reached
			    	sb.append(line); // Add every line of content  
			    }  
			    textDocContent = sb.toString();
	        } 
	        
	        if("</DOC>".equals(line)) { // If the end of a doc has been reached
	        	//System.out.println("textDocContent:" + textDocContent);
	        	textDoc.put(textDocID, textDocContent.toCharArray()); // Put the doc info into map
	        	                                                      // Return char array cos it can be easily be stemmed using Stemmer
		        return textDoc; 
	        }
	     }
	     
	     br.close();
	     return null;
	} 
}